package src.main.kotlin.day91

/**
 * 泛型函数
 *  泛型类的构造函数可以接受任何类型
 *
 */

fun main() {

    val box1:MagicBox2<boy2> = MagicBox2(boy2("jack", 20))
    val box2:MagicBox2<Dog2> = MagicBox2(Dog2(30))

    box1.available = false
    box1.fetch()?.run {
        println("you find a ${this.name}")
    }
}

class MagicBox2<T>(item:T){
    var available = false
    private var subject:T = item
    fun fetch(): T?{
        return subject.takeIf { available  }
    }
}

class boy2(val name: String,val age:Int)


class Dog2(val weight:Int)

